package com.ppp.poi.web;

import com.ppp.poi.bean.base.AppResult;
import com.ppp.poi.bean.entity.Region;
import com.ppp.poi.common.AppConstant;
import com.ppp.poi.service.RegionsService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 通用接口
 *
 * @author Wu Jin
 * @create 2020-01-19
 */
@RequestMapping("/common")
@RestController
public class CommonController {

  @Autowired
  private RegionsService regionsService;

  /**
   * 获取poi数据列表头
   */
  @GetMapping("/headers")
  public AppResult<Map<String, String>> headers() {
    Map<String, String> headerDict = AppConstant.HEADER_DICT;
    List<Map<String, String>> collect = headerDict.entrySet().stream().map(d -> {
      Map<String, String> m = new HashMap<>();
      m.put("code", d.getValue());
      m.put("type", d.getKey());
      return m;
    }).collect(Collectors.toList());
    return new AppResult(collect);
  }

  /**
   * 获取行政区
   */
  @GetMapping("/regions")
  public AppResult<List<Region>> regions() {
    return new AppResult(regionsService.getByParentId());
  }

  /**
   * 获取类型
   */
  @GetMapping("/types")
  public AppResult<Map<String, String>> types() {
    Map<String, String> typeDict = AppConstant.TYPE_DICT;
    List<Map<String, String>> collect = typeDict.entrySet().stream().map(d -> {
      Map<String, String> m = new HashMap<>();
      m.put("code", d.getValue());
      m.put("type", d.getKey());
      return m;
    }).collect(Collectors.toList());
    return new AppResult(collect);
  }

}
